﻿package mortal.game.view.chat.selectPanel
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;

    public class ColorSelector extends GSprite
    {
        private var colorArr:Array;
        private static var _instance:ColorSelector;
        private static var _currentBtn:InteractiveObject;
        private static var _dicbtnCallback:Dictionary = new Dictionary();

        public function ColorSelector()
        {
            this.colorArr = new Array(4290701566, 4278254847, 4294951571, 4294959616, 4294901988, 4294901760);
            return;
        }// end function

        override protected function configUI() : void
        {
            var _loc_1:int = 0;
            var _loc_4:ColorItem = null;
            super.configUI();
            _loc_1 = 0;
            var _loc_2:* = this.colorArr.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = new ColorItem(this.colorArr[_loc_3]);
                _loc_4.x = 0;
                _loc_4.y = _loc_1;
                _loc_1 = _loc_1 + _loc_4.height;
                _loc_4.addEventListener(EventName.ChatColorSelect, this.onChatColorSelectHandler);
                addChild(_loc_4);
                _loc_3++;
            }
            return;
        }// end function

        private function onChatColorSelectHandler(event:DataEvent) : void
        {
            (_dicbtnCallback[_currentBtn] as Function).call(null, event.data);
            hide();
            return;
        }// end function

        public static function get instance() : ColorSelector
        {
            if (!_instance)
            {
                _instance = new ColorSelector;
            }
            return _instance;
        }// end function

        public static function registBtn(param1:IFrUI, param2:Function) : void
        {
            _dicbtnCallback[param1] = param2;
            param1.configEventListener(MouseEvent.CLICK, onClickBtn);
            return;
        }// end function

        public static function unRegistBtn(param1:IFrUI) : void
        {
            if (param1)
            {
                (param1 as InteractiveObject).removeEventListener(MouseEvent.CLICK, onClickBtn);
                delete _dicbtnCallback[param1];
            }
            return;
        }// end function

        private static function onClickBtn(event:MouseEvent) : void
        {
            var _loc_3:Point = null;
            var _loc_2:* = event.target as InteractiveObject;
            _currentBtn = _loc_2;
            if (ColorSelector.instance.parent)
            {
                hide();
            }
            else
            {
                _loc_3 = _loc_2.parent.localToGlobal(new Point(_loc_2.x, _loc_2.y));
                ColorSelector.instance.x = _loc_3.x;
                ColorSelector.instance.y = _loc_3.y - 120;
                LayerManager.toolTipLayer.addChild(ColorSelector.instance);
                Global.instance.callLater(addFrameClickStage);
            }
            return;
        }// end function

        private static function hide() : void
        {
            ColorSelector.instance.parent.removeChild(ColorSelector.instance);
            Global.stage.removeEventListener(MouseEvent.CLICK, onClickStage);
            return;
        }// end function

        private static function addFrameClickStage() : void
        {
            Global.stage.addEventListener(MouseEvent.CLICK, onClickStage);
            return;
        }// end function

        private static function onClickStage(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as DisplayObject;
            if (_loc_2 != _currentBtn && _loc_2 != ColorSelector.instance && !ColorSelector.instance.contains(_loc_2))
            {
                if (ColorSelector.instance.parent)
                {
                    hide();
                }
            }
            return;
        }// end function

    }
}
